/*	
	
	Copyright (c) 2007-2009 Ryan Christiansen
	
	This software is provided 'as-is', without any express or implied
	warranty. In no event will the authors be held liable for any damages
	arising from the use of this software.
	
	Permission is granted to anyone to use this software for any purpose,
	including commercial applications, and to alter it and redistribute it
	freely, subject to the following restrictions:
	
	1. The origin of this software must not be misrepresented; you must not
	claim that you wrote the original software. If you use this software
	in a product, an acknowledgment in the product documentation would be
	appreciated but is not required.
	
	2. Altered source versions must be plainly marked as such, and must not be
	misrepresented as being the original software.
	
	3. This notice may not be removed or altered from any source
	distribution.
	
*/
package funit.framework
{
	import flash.utils.Dictionary;
	
	import funit.errors.AssertionError;
	import funit.framework.writers.MessageWriter;
	
	import mx.collections.IList;
	
	public class CollectionAssert
	{
		
		/**
		 * @param enumerable
		 */
		private static function isCollection( enumerable:Object ) : Boolean
		{
			return ((enumerable is IList) || (enumerable is Array)) ? true : false;
		}
		
		/**
		 * @param enumerable
		 * @return
		 */
		private static function getLength( enumerable:Object ) : int
		{
			if (enumerable is Array)
			{
				return (enumerable as Array).length;
			}
			else if (enumerable is IList)
			{
				return IList(enumerable).length;
			}
			else
			{
				return -1;
			}
		}
		
		
		/**
		 * @param enumerable
		 * @param definition
		 * @param message
		 * @param messageParameters
		 */
		public static function allItemsAreInstancesOf( enumerable:Object, definition:Class, message:String = "", ... messageParameters ) : void
		{
			for each ( var value:Object in enumerable )
			{
				if (!(value is definition))
				{
					var writer:MessageWriter = new MessageWriter( message, messageParameters );
					writer.writeExpectedLine(definition, "all items instance of");
					writer.writeActualLine(enumerable);
					
					throw new AssertionError( writer.toString() );
				}
			}
		}
		
		/**
		 * @param enumerable
		 * @param message
		 * @param messageParameters
		 */
		public static function allItemsAreNotNull( enumerable:Object, message:String = "", ... messageParameters ) : void
		{
			for each ( var value:Object in enumerable )
			{
				if (value == null)
				{
					var writer:MessageWriter = new MessageWriter( message, messageParameters );
					writer.writeExpectedLine(null, "all items not");
					writer.writeActualLine(enumerable);
					
					throw new AssertionError( writer.toString() );
				}
			}
		}
		
		/**
		 * @param enumerable
		 * @param message
		 * @param messageParameters
		 */
		public static function allItemsAreUnique( enumerable:Object, message:String = "", ... messageParameters ) : void
		{
			var valueCache:Dictionary = new Dictionary();
			
			for each ( var value:Object in enumerable )
			{
				if (valueCache[value] == true)
				{
					var writer:MessageWriter = new MessageWriter( message, messageParameters );
					writer.writeLine(MessageString.ExpectedPrefix + "all items unique");
					writer.writeActualLine(enumerable);
					
					throw new AssertionError( writer.toString() );
				}
				
				valueCache[value] = true;
			}
		}
		
		/**
		 * @param enumerable
		 * @param instance
		 * @param message
		 * @param messageParameters
		 */
		public static function contains( enumerable:Object, instance:Object, message:String = "", ... messageParameters ) : void
		{
			for each ( var value:Object in enumerable )
			{
				if (value == instance)
				{
					return;
				}
			}
			
			var writer:MessageWriter = new MessageWriter( message, messageParameters );
			writer.writeExpectedLine(instance, "collection containing");
			writer.writeActualLine(enumerable);
			
			throw new AssertionError( writer.toString() );
		}
		
		/**
		 * @param enumerable
		 * @param instance
		 * @param message
		 * @param messageParameters
		 */
		public static function doesNotContain( enumerable:Object, instance:Object, message:String = "", ... messageParameters ) : void
		{
			for each ( var value:Object in enumerable )
			{
				if (value == instance)
				{
					var writer:MessageWriter = new MessageWriter( message, messageParameters );
					writer.writeExpectedLine(instance, "collection not containing");
					writer.writeActualLine(enumerable);
					
					throw new AssertionError( writer.toString() );
				}
			}
		}
		
		/**
		 * @param enumerable
		 * @param message
		 * @param messageParameters
		 */
		public static function isEmpty( enumerable:Object, message:String = "", ... messageParameters ) : void
		{
			if ( getLength(enumerable) != 0 )
			{
				var writer:MessageWriter = new MessageWriter( message, messageParameters );
				writer.writeExpectedLine(new Array());
				writer.writeActualLine(enumerable);
				
				throw new AssertionError( writer.toString() );
			}
		}
		
		/**
		 * @param enumerable
		 * @param message
		 * @param messageParameters
		 */
		public static function isNotEmpty( enumerable:Object, message:String = "", ... messageParameters ) : void
		{
			if ( getLength(enumerable) > 0 )
			{
				return;
			}
			
			var writer:MessageWriter = new MessageWriter( message, messageParameters );
			writer.writeExpectedLine(new Array(), "not");
			writer.writeActualLine(enumerable);
			
			throw new AssertionError( writer.toString() );
		}
		
		/**
		 * @param expected
		 * @param actual
		 * @param message
		 * @param messageParameters
		 */
		public static function areEqual( expected:Object, actual:Object, message:String = "", ... messageParameters ) : void
		{
			Assert.areEqual(expected, actual, message, messageParameters);
		}
		
		/**
		 * @param expected
		 * @param actual
		 * @param message
		 * @param messageParameters
		 */
		public static function areNotEqual( expected:Object, actual:Object, message:String = "", ... messageParameters ) : void
		{
			Assert.areNotEqual(expected, actual, message, messageParameters);
		}
		
		/**
		 * @param expected
		 * @param actual
		 * @param message
		 * @param messageParameters
		 */
		public static function areEquivalent( expected:Object, actual:Object, message:String = "", ... messageParameters ) : void
		{
			var isFailure:Boolean = false;
			
			assert:
			{
				if ( getLength(expected) != getLength(actual) )
				{
					isFailure = true;
					
					break assert;
				}
				
				var expectedIndexer:CollectionIndexer = new CollectionIndexer(expected);
				var actualIndexer:CollectionIndexer = new CollectionIndexer(actual);
				
				for each ( var value:Object in actualIndexer.values )
				{
					if (expectedIndexer.getCount(value) != actualIndexer.getCount(value))
					{
						isFailure = true;
						
						break assert;
					}
				}
			}
			
			if (isFailure)
			{
				var writer:MessageWriter = new MessageWriter( message, messageParameters );
				writer.writeExpectedLine(expected, "equivalent to");
				writer.writeActualLine(actual);
				
				throw new AssertionError( writer.toString() );
			}
		}
		
		/**
		 * @param expected
		 * @param actual
		 * @param message
		 * @param messageParameters
		 */
		public static function areNotEquivalent( expected:Object, actual:Object, message:String = "", ... messageParameters ) : void
		{
			if ( getLength(expected) != getLength(actual) )
			{
				return;
			}
			
			var expectedIndexer:CollectionIndexer = new CollectionIndexer(expected);
			var actualIndexer:CollectionIndexer = new CollectionIndexer(actual);
			
			for each ( var value:Object in actualIndexer.values )
			{
				if (expectedIndexer.getCount(value) != actualIndexer.getCount(value))
				{
					return;
				}
			}
			
			var writer:MessageWriter = new MessageWriter( message, messageParameters );
			writer.writeExpectedLine(expected, "not equivalent to");
			writer.writeActualLine(actual);
			
			throw new AssertionError( writer.toString() );
		}
		
		/**
		 * @param subset
		 * @param superset
		 * @param message
		 * @param messageParameters
		 */
		public static function isSubsetOf( subset:Object, superset:Object, message:String = "", ... messageParameters ) : void
		{
			var isFailure:Boolean = false;
			
			assert:
			{
				if ( getLength(subset) > getLength(superset) )
				{
					isFailure = true;
					
					break assert;
				}
				
				var subsetIndexer:CollectionIndexer = new CollectionIndexer(subset);
				var supersetIndexer:CollectionIndexer = new CollectionIndexer(superset);
				
				for each ( var value:Object in subsetIndexer.values )
				{
					if (supersetIndexer.getCount(value) == 0)
					{
						isFailure = true;
						
						break assert;
					}
					else if (subsetIndexer.getCount(value) > supersetIndexer.getCount(value))
					{
						isFailure = true;
						
						break assert;
					}
				}
			}
			
			if (isFailure)
			{
				var writer:MessageWriter = new MessageWriter( message, messageParameters );
				writer.writeExpectedLine(superset, "subset of");
				writer.writeActualLine(subset);
				
				throw new AssertionError( writer.toString() );
			}
		}
		
		/**
		 * @param subset
		 * @param superset
		 * @param message
		 * @param messageParameters
		 */
		public static function isNotSubsetOf( subset:Object, superset:Object, message:String = "", ... messageParameters ) : void
		{
			if ( getLength(subset) > getLength(superset) )
			{
				return;
			}
			
			var subsetIndexer:CollectionIndexer = new CollectionIndexer(subset);
			var supersetIndexer:CollectionIndexer = new CollectionIndexer(superset);
			
			for each ( var value:Object in subsetIndexer.values )
			{
				if (supersetIndexer.getCount(value) == 0)
				{
					return;
				}
				else if (subsetIndexer.getCount(value) > supersetIndexer.getCount(value))
				{
					return;
				}
			}
			
			var writer:MessageWriter = new MessageWriter( message, messageParameters );
			writer.writeExpectedLine(superset, "not subset of");
			writer.writeActualLine(subset);
			
			throw new AssertionError( writer.toString() );
		}
		
	}
	
}

import flash.utils.Dictionary;

internal class CollectionIndexer
{
	// Dictionary key for 'null' values
	private static const NULL:Object = new Object();
	
	private var _hash:Dictionary = new Dictionary();
	private var _values:Array = new Array();
	
	public function CollectionIndexer( collection:Object )
	{
		var value:Object;
		
		for each (value in collection)
		{
			if (value == null)
			{
				value == NULL;
			}
			
			_hash[value] = getCount(value) + 1;
		}
		
		for (value in _hash)
		{
			_values.push(value);
		}
	}
	
	
	public function getCount( value:Object ) : int
	{
		return (_hash[value] != null) ? int(_hash[value]) : 0;
	}
	
	
	public function get values() : Array
	{
		return _values;
	}
}
